package com.twoGroup.boot.service.imp;


import com.twoGroup.boot.controller.AddressController;
import com.twoGroup.boot.mapper.CartMapper;
import com.twoGroup.boot.pojo.CartAndShopAndUserAndGoods;
import com.twoGroup.boot.pojo.Page;
import com.twoGroup.boot.service.CartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    public static Logger logger = LoggerFactory.getLogger(AddressController.class);

    @Autowired
    private CartMapper cartMapper;


    @Override
    public int selectGoodsStock(Integer goodsId) {
        return cartMapper.selectGoodsStock(goodsId);
    }

    @Transactional
    @Override
    public int updateGoodsStock(Integer goodsId, Integer goodsStock) {
        return cartMapper.updateGoodsStock(goodsId, goodsStock);
    }

    @Override
    public int selectGoodsCart(Integer userId, Integer goodsId) {
        return cartMapper.selectGoodsCart(userId, goodsId);
    }

    @Override
    public int selectGoodsCount(Integer userId, Integer goodsId) {
        return cartMapper.selectGoodsCount(userId,goodsId);
    }

    @Transactional
    @Override
    public int addGoods(Integer userId, Integer goodsId, Integer shopId, Integer goodsCount) {
        try {
            if(goodsCount <= cartMapper.selectGoodsStock(goodsId)){
                logger.info("先判断加入的数量<库存数");
                //判断用户是否已经添加过该商品
                if(cartMapper.selectGoodsCart(userId,goodsId)==1){
                    logger.info("判断用户是否已经添加过该商品");
                    //判断添加的商品数和数据库内的商品数的数量关系 如果小于等于，走下一步
                    if(cartMapper.selectGoodsCount(userId, goodsId)+goodsCount<=cartMapper.selectGoodsStock(goodsId)){
                        //商品库存-goodsCount
//                        cartMapper.updateGoodsStock(goodsId,goodsCount);
                        //购物车内该数量+goodsCount
                        cartMapper.updateGoodsCart(userId,goodsId,goodsCount);
                        logger.info("该商品已存在，调用了更新购物车的方法");
                        return 2;
                    }
                    //如果数据库没该数据 那么添加到购物车
                }else{
//                    cartMapper.updateGoodsStock(goodsId,goodsCount);
                    cartMapper.addGoods(userId, goodsId,shopId,goodsCount);
                    logger.info("该商品不存在，调用了添加购物车的方法");
                    return 1;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Transactional
    @Override
    public int deleteGoodsCart(Integer userId, Integer goodsId) {
        return cartMapper.deleteGoodsCart(userId,goodsId);
    }

    @Transactional
    @Override
    public int deleteAllGoodsCart(Integer userId) {
        return cartMapper.deleteAllGoodsCart(userId);
    }

    @Transactional
    @Override
    public int updateGoodsCart(Integer userId, Integer goodsId, Integer goodsCount) {
        return cartMapper.updateGoodsCart(userId,goodsId,goodsCount);
    }

    @Override
    public Page<CartAndShopAndUserAndGoods> queryUserCart(Integer userId, Integer pageNow, Integer pageSize) {
        Page<CartAndShopAndUserAndGoods> page = new Page<>();
        page.setPageTotalCount(cartMapper.userCartAllCount(userId));
        page.setPageNow(pageNow);
        page.setPageSize(pageSize);
        page.setPageTotal(page.getPageTotal());
        page.setStart((pageNow-1)*pageSize);
        List<CartAndShopAndUserAndGoods> list = cartMapper.queryUserCart(userId, page.getStart(), pageSize);
        page.setList((ArrayList<CartAndShopAndUserAndGoods>) list);
        return page;
    }

    @Override
    public CartAndShopAndUserAndGoods queryUserCartById(Integer userId, Integer goodsId) {
        return cartMapper.queryUserCartById(userId, goodsId);
    }

    @Override
    public CartAndShopAndUserAndGoods selectByUserId(Integer userId) {
        return cartMapper.selectByUserId(userId);
    }
}
